define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'post/post/index',
                    add_url: 'post/post/add',
                    edit_url: 'post/post/edit',
                    del_url: 'post/post/del',
                    recycle_url: 'post/post/recycle',
                    multi_url: 'post/post/multi',
                    table: 'post',
                }
            });

            var table = $("#table");
            Fast.config.openArea = ['100%','100%'];

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'post.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: false },
                        { field: 'title', title: __('title'), operate: 'LIKE' },
                        // { field: 'content', title: __('content'), operate: 'LIKE',},
                        { field: 'category.name', title: __('cateid'), },
                        { field: 'point', title: __('point'), operate: false },
                        { field: 'business.nickname', title: __('busid'), operate: 'LIKE' },
                        { field: 'accepted.nickname', title: __('accept'), searchable: false },
                        { field: 'status', title: __('status'), searchList: { "0": __('未采纳'), "1": __('已采纳') }, formatter: Table.api.formatter.flag },
                        { field: 'createtime', title: __('createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        }
                    ]
                ],
                // escape:false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#accept_text').click(function (){
                Backend.api.open("post/post/business", "客户", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        console.log(data);
                        $('#accept_text').val(data.nickname)
                        $('#accept').val(data.id)
                    }
                })
            })
            $('#busid_text').click(function (){
                Backend.api.open("post/post/business", "客户", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        console.log(data);
                        $('#busid_text').val(data.nickname)
                        $('#busid').val(data.id)
                    }
                })
            })
            Controller.api.bindevent();
        },
        business:function()
        {
            Table.api.init({
            });
            // console.log(business.id);
            var businessTable = $("#table");
            businessTable.bootstrapTable({
                url: 'post/post/business',
                pk: 'business.id',
                sortName: 'business.id',
                search: false,
                // 关闭导出
                showExport: false,
                // 关闭通用搜索
                commonSearch: false,
                // 关闭显示的列
                showColumns: false,
                // 关闭切换显示
                showToggle: false,
                columns: [
                    [
                        // {checkbox: true},
                        { field: 'id', title: __('Id'), searchable: true },
                        { field: 'mobile', title: __('Mobile'), operate: 'LIKE' },
                        { field: 'nickname', title: __('Nickname'), operate: 'LIKE' },
                        { field: 'avatar_text', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false },
                        {
                            field: 'gender', title: __('Gender'), searchList: { '0': __('Gender 0'), '1': __('Gender 1'), '2': __('Gender 2') },
                            operate: 'FIND_IN_SET',
                            formatter: Table.api.formatter.normal
                        },
                        { field: 'source.name', title: __('Source'), operate: 'LIKE' },
                        { field: 'email', title: __('Email'), operate: 'LIKE' },
                        // 日期搜索格式化
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: businessTable,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'chapter', text: '选择',
                                    title: '选择',
                                    classname: 'btn btn-primary btn-xs business',
                                },
                            ]
                        }
                    ]
                ]
            });
            // 点击选择
            $('#table').on('click', '.business', function () {
                let index = $(this).parents('tr').data('index')
                let data = Table.api.getrowdata(businessTable, index);
                // console.log(data);
                Backend.api.close(data)
            })
            // 为表格绑定事件
            Table.api.bindevent(businessTable);
        },
        edit: function () {
            $('#accept_text').click(function (){
                Backend.api.open("post/post/business", "客户", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        $('#accept_text').val(data.nickname)
                        $('#accept').val(data.id)
                    }
                })
            })
            $('#busid_text').click(function (){
                Backend.api.open("post/post/business", "客户", {
                    area: ['80%', '80%'],
                    callback: function (data) {
                        $('#busid_text').val(data.nickname)
                        $('#busid').val(data.id)

                    }
                })
            })
            Controller.api.bindevent();
        },
        recycle: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'post/post/recycle',
                    restore_url: 'post/post/restore',
                    del_url: 'post/post/destroy',
                    table: 'post',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'post.id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id'), sortable: false },
                        { field: 'title', title: __('title'), operate: 'LIKE' },
                        // { field: 'content', title: __('content'), operate: 'LIKE',},
                        { field: 'category.name', title: __('cateid'), },
                        { field: 'point', title: __('point'), operate: false },
                        { field: 'business.nickname', title: __('busid'), operate: 'LIKE' },
                        { field: 'accepted.nickname', title: __('accept'), searchable: false },
                        { field: 'status', title: __('status'), searchList: { "0": __('未采纳'), "1": __('已采纳') }, formatter: Table.api.formatter.flag },
                        { field: 'createtime', title: __('createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'restore',
                                    title: function (row) {
                                        return `数据恢复`
                                    },
                                    icon: 'fa fa-reply',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: $.fn.bootstrapTable.defaults.extend.restore_url,
                                    confirm: '是否还原该帖子？',
                                    extend: "data-toggle='tooltip'",
                                    success: function (data) {
                                        //ajax成功会刷新一下table数据列表
                                        table.bootstrapTable('refresh');
                                    }
                                }
                            ]
                        }
                    ]
                ],
                escape: false,
                // search:false,
            });

            $('.btn-restore').on('click', function () {
                let ids = Table.api.selectedids(table);
                ids = ids.toString()
                // console.log(ids);
                // return false;
                layer.confirm('确定要还原吗?', { title: '还原', btn: ['是', '否'] },
                    function (index) {
                        layer.close(index);
                        $.post("post/post/restore", { ids: ids, action: 'success', reply: '' }, function (response) {
                            if (response.code == 1) {
                                Toastr.success(response.msg)
                                $(".btn-refresh").trigger('click');
                            } else {
                                Toastr.error(response.msg)
                            }
                        }, 'json')
                    },
                    function (index) {
                        layer.close(index);
                    }
                );

            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        del: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});